var fActiveSnip = null;
var fActiveLink = null;
var fCache = {};

function hideActiveSnip(aClear)
{
	if (fActiveSnip != null)
	{
		var obj = document.getElementById(fActiveSnip);
		if (obj)
		{
			obj.style.display = "none";
			if (aClear)
			{
				var nodes = obj.childNodes;
				if (nodes && nodes.length > 0)
				{
					var nLength = nodes.length;
					for (var i = 0; i < nLength; i++)
					{
						obj.removeChild(obj.firstChild);
					}
				}
			}
			var nodes = obj.parentNode.getElementsByTagName("a");
			var node = nodes.length > 0 ? nodes[0] : null;
			if (node)
			{
				node.className = "default";
			}
		}
	}
}

function showSnip(aSnip)
{
	hideActiveSnip();
	if (aSnip !== fActiveSnip)
	{
		var obj = document.getElementById(aSnip);
		if (obj)
		{
			var nodes = obj.parentNode.getElementsByTagName("a");
			var node = nodes.length > 0 ? nodes[0] : null;
			if (node)
			{
				node.className = "active";
			}
			obj.style.display = "block";
			fActiveSnip = aSnip;
		}
	}
	else
	{
		fActiveSnip = null;
	}
}

function getAbsPos(aElement, which)
{
 var iPos = 0;

 while (aElement)
 {
   iPos += aElement["offset" + which];
   aElement = aElement.offsetParent;
 }
 return iPos;
}

function showCodeSnips(aSnip, anAnchor, aText)
{
	hideActiveSnip(true);
	if (aSnip != fActiveSnip)
	{
		var obj = document.getElementById(aSnip);
		if (obj)
		{
			obj.innerHTML = "<div class='code_snip_content'>var jTools= new JsonTools();<br>" + aText + "</div>" +
			                "<a class='read_more' href='javascript:loadExample(&quot;" + anAnchor + "&quot;, &quot;" + aSnip +
			                "&quot;)'>Get more examples...</a>";
			var nodes = obj.parentNode.getElementsByTagName("a");
			var node = nodes.length > 0 ? nodes[0] : null;
			if (node)
			{
				node.className = "active";
			}
			obj.style.display = "block";
			fActiveSnip = aSnip;

			window.setTimeout( function()
			{
				try
				{
					var html = document.getElementsByTagName("HTML")[0];
					var pos = getAbsPos(node,"Top");
					if ( (pos - html.scrollTop) < 0 )
					{
						html.scrollTop = pos - 40;
					}
				}
				catch(ex)
				{
					
				}
			}, 200);
		}
	}
	else
	{
		fActiveSnip = null;
	}
}

/********************* Show document fragment from JS doc ***************/

var DOC_URL = "html/jsoner.html";

function showDocument(aSnip, anAnchor)
{
	hideActiveSnip(true);
	if (aSnip != fActiveSnip)
	{
		var data = fCache[DOC_URL];
		if ( data )
		{
		  parseDocument(aSnip, anAnchor, data);
		}
		else
		{
			var loader = new Transporter(function()
			{
				var rData = this.getResponsedText();
				if ( rData )
				{
					fCache[DOC_URL] = rData;
				}
				parseDocument(aSnip, anAnchor, rData);
			})
			loader.loadData(DOC_URL);
		}
	}
	else
	{
		fActiveSnip = null;
	}
}

function parseDocument(aSnip, anAnchor, aData)
{
	var first = aData.indexOf("<A NAME=\"" + anAnchor + "\">");
	if (first > 0)
	{
		var last = aData.indexOf("<A NAME=", first + 1);
		if (last < 0)
		{
			last = aData.length;
		}
		var obj = document.getElementById(aSnip);
		if (obj)
		{
			obj.innerHTML = aData.substring(first, last);

			var nodes = obj.parentNode.getElementsByTagName("a");
			var node = nodes.length > 0 ? nodes[0] : null;
			if (node)
			{
				node.className = "active";
			}
			obj.style.display = "block";
			fActiveSnip = aSnip;
		}
	}
}

/******************* Show document fragment from Examples **************************/

var EXM_URL = "examples.htm";

function loadExample(anAnchor, aDiv)
{
	var data = fCache[EXM_URL];
	if ( data )
	{
		parseExample(anAnchor, aDiv, data)
	}
	else
	{
		var loader = new Transporter(function()
		{
			var rData = this.getResponsedText();
			parseExample(anAnchor, aDiv, rData);
		})
		loader.loadData(EXM_URL);
	}
}

function parseExample(anAnchor, aDiv, aData)
{
	var first = aData.indexOf("<a name=\"" + anAnchor + "\">");
	if (first > 0)
	{
		var last = aData.indexOf("<a name=\"" + anAnchor + "End\">", first + 1);
		if (last < 0)
		{
			last = aData.length;
		}
		var obj = document.getElementById(aDiv);
		if (obj)
		{
			var str = aData.substring(first, last);
			var form = document.getElementById("sample_form");
			if (form && str.indexOf("sample_form") > 0)
			{
				form.parentNode.removeChild(form);
			}
			obj.innerHTML = str;
		}
	}
}

/*******************************************************/

function loadContent(aURL, aCallBack)
{
	if (aURL != fActiveLink)
	{
		var loader = new Transporter(function()
		{
			if (fActiveLink)
			{
				var link = document.getElementById(fActiveLink);
				if (link)
				{
					link.className = "default";
				}
			}

			var link = document.getElementById(aURL);
			if (link)
			{
				link.className = "active";
				var hr = link.getAttribute("hr");
				var head = document.getElementById("header");
				if (head)
				{
					head.innerHTML = hr;
				}
			}
			fActiveLink = aURL;

			var data = this.getResponsedText();
			var obj = document.getElementById("main");
			if (obj)
			{
				obj.innerHTML = data;
			}
		})
		loader.loadData(aURL);
	}
}

var newwindow;
function showNewWindow(url)
{
	newwindow = window.open(url,'Example','height=400,width=200');
	if (window.focus)
	{
		newwindow.focus()
	}
}
